<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    let allMenuTreeList = [
        {
            "key": "999",
            "upperMenuId": "9999",
            "title": "系统菜单",
            "children": [
                {
                    "key": "4",
                    "upperMenuId": "999",
                    "title": "权限管理",
                    "children": [
                        {
                            "key": "43",
                            "upperMenuId": "4",
                            "title": "人员权限配置",
                            "children": null
                        },
                        {
                            "key": "44",
                            "upperMenuId": "4",
                            "title": "人员权限查询",
                            "children": null
                        },
                        {
                            "key": "45",
                            "upperMenuId": "4",
                            "title": "岗位权限配置",
                            "children": null
                        },
                        {
                            "key": "47",
                            "upperMenuId": "4",
                            "title": "标签权限管理",
                            "children": null
                        }
                    ]
                },
                {
                    "key": "7",
                    "upperMenuId": "999",
                    "title": "风控管理",
                    "children": [
                        {
                            "key": "71",
                            "upperMenuId": "7",
                            "title": "系统监控日志",
                            "children": null
                        }
                    ]
                },
                {
                    "key": "8",
                    "upperMenuId": "999",
                    "title": "车商看板管理",
                    "children": [
                        {
                            "key": "81",
                            "upperMenuId": "8",
                            "title": "看板清单查询",
                            "children": null
                        },
                        {
                            "key": "82",
                            "upperMenuId": "8",
                            "title": "车商业务看板",
                            "children": null
                        },
                        {
                            "key": "83",
                            "upperMenuId": "8",
                            "title": "新车销量导入",
                            "children": null
                        },
                        {
                            "key": "84",
                            "upperMenuId": "8",
                            "title": "保费清单下载",
                            "children": null
                        },
                        {
                            "key": "85",
                            "upperMenuId": "8",
                            "title": "代码单清单下载",
                            "children": null
                        },
                        {
                            "key": "86",
                            "upperMenuId": "8",
                            "title": "经销商评分管理",
                            "children": null
                        },
                        {
                            "key": "87",
                            "upperMenuId": "8",
                            "title": "经销商级别管理",
                            "children": null
                        },
                        {
                            "key": "88",
                            "upperMenuId": "8",
                            "title": "营销活动管理",
                            "children": null
                        },
                        {
                            "key": "89",
                            "upperMenuId": "8",
                            "title": "维修产值清单下载 ",
                            "children": null
                        },
                        {
                            "key": "810",
                            "upperMenuId": "8",
                            "title": "目标导入",
                            "children": null
                        }
                    ]
                },
                {
                    "key": "10",
                    "upperMenuId": "999",
                    "title": "经销商管理",
                    "children": [
                        {
                            "key": "101",
                            "upperMenuId": "10",
                            "title": "经销商信息管理",
                            "children": null
                        },
                        {
                            "key": "102",
                            "upperMenuId": "10",
                            "title": "经销商集团信息管理",
                            "children": null
                        },
                        {
                            "key": "103",
                            "upperMenuId": "10",
                            "title": "经销商业务信息审核",
                            "children": null
                        }
                    ]
                }
            ]
        }
    ];

    let allButtonMap = {
        "44": [],
        "88": [
            {
                "key": "211",
                "upperMenuId": "88",
                "title": "新增",
                "children": null
            },
            {
                "key": "212",
                "upperMenuId": "88",
                "title": "下载模板",
                "children": null
            },
            {
                "key": "213",
                "upperMenuId": "88",
                "title": "导入数据",
                "children": null
            }
        ],
        "45": [],
        "89": [
            {
                "key": "214",
                "upperMenuId": "89",
                "title": "导出数据",
                "children": null
            }
        ],
        "47": [],
        "101": [],
        "102": [],
        "103": [],
        "810": [
            {
                "key": "215",
                "upperMenuId": "810",
                "title": "下载模板",
                "children": null
            },
            {
                "key": "216",
                "upperMenuId": "810",
                "title": "导入数据",
                "children": null
            }
        ],
        "81": [
            {
                "key": "201",
                "upperMenuId": "81",
                "title": "导出",
                "children": null
            }
        ],
        "71": [],
        "82": [],
        "83": [
            {
                "key": "202",
                "upperMenuId": "83",
                "title": "下载模板",
                "children": null
            },
            {
                "key": "203",
                "upperMenuId": "83",
                "title": "导入数据",
                "children": null
            }
        ],
        "84": [
            {
                "key": "204",
                "upperMenuId": "84",
                "title": "导出",
                "children": null
            }
        ],
        "85": [
            {
                "key": "205",
                "upperMenuId": "85",
                "title": "导出",
                "children": null
            }
        ],
        "86": [
            {
                "key": "206",
                "upperMenuId": "86",
                "title": "下载模板",
                "children": null
            },
            {
                "key": "207",
                "upperMenuId": "86",
                "title": "导出数据",
                "children": null
            }
        ],
        "43": [],
        "87": [
            {
                "key": "208",
                "upperMenuId": "87",
                "title": "下载模板",
                "children": null
            },
            {
                "key": "209",
                "upperMenuId": "87",
                "title": "导入数据",
                "children": null
            },
            {
                "key": "210",
                "upperMenuId": "87",
                "title": "导出数据",
                "children": null
            }
        ]
    }



    const getButtonMap = (key,allButtonMap) => {
        return allButtonMap[key] || []
    }



    const mergeData = (list) => {
        let res = [];
        list.forEach(item => {
            res.push({
                ...item,
                children: (item.children && item.children.length > 0) && mergeData(item.children),
                buttonList: getButtonMap(item.key)
            })
        })

        return res;
    }

    let res = mergeData(allMenuTreeList)
    console.log("结果：\n", res)

</script>

</html>